package com.show.controller;

import com.alibaba.fastjson.JSON;
import com.show.pojo.Trainee;
import com.show.pojo.User;
import com.show.service.TraineeService;
import com.show.service.impl.TraineeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 绛河
 */
@WebServlet("/trainee/*")
public class TraineeController extends BaseServlet {

    private TraineeService traineeService = new TraineeServiceImpl();

    public void searchAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Trainee> trainees = traineeService.searchAll();
        String jsonString = JSON.toJSONString(trainees);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void searchBefore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LocalDateTime localDateTime = LocalDateTime.now();
        localDateTime = localDateTime.minusMonths(36L);
        Integer integer = traineeService.searchBefore(localDateTime);
        String jsonString = JSON.toJSONString(integer);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void searchByTerm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String _term = request.getParameter("term");
        Integer term = Integer.parseInt(_term);
        Integer integer = traineeService.searchByTerm(term);
        String jsonString = JSON.toJSONString(integer);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void searchByYear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Map<String, Object>> maps = traineeService.searchByYear();
        String jsonString = JSON.toJSONString(maps);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }


    public void searchByMonth(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Map<String, Object>> maps = traineeService.searchByMonth(2023);
        String jsonString = JSON.toJSONString(maps);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void saveTrainee(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader br = request.getReader();
        String params = br.readLine();
        Trainee trainee = JSON.parseObject(params, Trainee.class);
        if (trainee!= null){
            Integer integer = traineeService.saveTrainee(trainee);
            String jsonString = JSON.toJSONString(integer);
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        }

    }


}
